package com.trkj.controller.hjj;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.Affaircategory;
import com.trkj.pojo.Attendancerule;
import com.trkj.pojo.Holiday;
import com.trkj.pojo.Shift;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Timecard;
import com.trkj.service.ServiceException;
import com.trkj.service.hjj.DataDatumService;
import com.trkj.vo.Result_AddRule_Result;

/**
 * 资料
 * @author HJJ
 *
 */
@Controller
public class DataDatumController {
	@Autowired
	private DataDatumService dataDatumService;//资料Service
	/**
	 * 查询所有的考勤卡
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_alltime_card")
	@ResponseBody
	public Map findAllTimeCard(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page,rows);
		dataDatumService.findAllTimeCard();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 根据考勤卡号查询
	 * @param page
	 * @param rows
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("to_byId_time_card")
	@ResponseBody
	public Map  findTimeCardById(int page,int rows,@Param(value = "timecardId")Integer timecardId){
		
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		dataDatumService.findTimeCardById(timecardId);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
 	}
	/**
	 *新增考勤卡
	 * @param t
	 * @return
	 */
	@RequestMapping("is_add_timecard")
	@ResponseBody
	public Result_AddRule_Result inserTimeCard(HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			boolean b = dataDatumService.inserTimeCard(staff.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 加载考勤卡
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("is_load_timecard")
	@ResponseBody
	public Timecard loadTimecard(Long timecardId){
		return dataDatumService.loadTimecard(timecardId);
	}
	
	/**
	 * 修改考勤卡状态
	 * @param 
	 * @param 
	 * @return
	 */
	@RequestMapping("update_timecardstate")
	@ResponseBody
	public Result_AddRule_Result updateTimeCardState_(Long timecardId,Long timecardZt){
		try {
			
			boolean b = dataDatumService.updateTimecard_state(timecardId, timecardZt);
			if(b==true){
				return Result_AddRule_Result.SUCCESS;
			}else{
				return Result_AddRule_Result.failure("已有员工持有");
			}
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 删除考勤卡
	 * @param 
	 * @param 
	 * @return
	 */
	@RequestMapping("del_timecardstate")
	@ResponseBody
	public Result_AddRule_Result delTimeCardState_(Long timecardId){
		try {
			
			boolean b = dataDatumService.delTimecard_state(timecardId);
			if(b==true){
				return Result_AddRule_Result.SUCCESS;
			}else{
				return Result_AddRule_Result.failure("已有员工持有");
			}
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	
	@RequestMapping("to_shift_all")
	@ResponseBody
	/**
	 * 查询所有的班次json格式
	 * @return
	 */
	public Map findAllShift(int page,int rows){
		long total =dataDatumService.pageByCountShift();//总记录数
		List<Shift> list = dataDatumService.pageShift(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	/**
	 * 查询所有的班次json格式
	 * @return
	 */
	@RequestMapping("to_isshiftByName")
	@ResponseBody
	public Map findisShift(String shiftName,int page,int rows){
		if(shiftName==null||"".equals(shiftName)){
			long total =dataDatumService.pageByCountShift();//总记录数
			List<Shift> list = dataDatumService.pageShift(page, rows);
			Map map = new HashMap<>();
			map.put("total", total);
			map.put("rows", list);
			return map;
		}else{
			
			List<Shift> list = dataDatumService.pageShiftByName(shiftName,page, rows);
			Map map = new HashMap<>();
			/*map.put("total", total);*/
			map.put("rows", list);
			return map;
		}
	}
	@RequestMapping("to_isholiday")
	@ResponseBody
	/**
	 * 查询所有的假种  json格式
	 * @return
	 */
	/*public Map findAllHoliday(int page,int rows){
		long total =dataDatumService.pageByCountisHoliday();//总记录数
		List<Holiday> list = dataDatumService.pageHoliday(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}*/
	public JSONArray findAllHoliday(){
		return dataDatumService.pageHoliday();
		
	}
	/**
	 * 查询所有的假种json格式
	 * @return
	 */
	@RequestMapping("to_isholidayByName")
	@ResponseBody
	public Map findisHoliday(String holidayName,int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		dataDatumService.pageHolidayByName(holidayName);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 新增假种
	 * @param aff
	 * @return
	 */
	@RequestMapping("is_add_holidayss")
	@ResponseBody
	public Result_AddRule_Result addHoliday(Holiday aff,HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			boolean b = dataDatumService.addHoliday(aff,staff.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	/**
	 * 根据编号删除假种
	 * @param attruId
	 * @return
	 */
	@RequestMapping("delete_holiday_")
	@ResponseBody
	public Result_AddRule_Result deleteHoliday(Long holidayId){
		try {
			boolean b= dataDatumService.delHoliday(holidayId);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	/**
	 * 加载假种
	 */
	@RequestMapping("load_holiday")
	@ResponseBody
	public Holiday loadHoliday(Long holidayId){
		return dataDatumService.loadHoliday(holidayId);
	}
	/**
	 * 修改假种
	 * @param affcaId
	 * @param aff
	 * @return
	 */
	@RequestMapping("is_update_holiday")
	@ResponseBody
	public Result_AddRule_Result updateHoliday(Long holidayId,Holiday holiday){
		try {
			boolean b = dataDatumService.updateHoliday(holidayId, holiday);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 修改假种状态
	 * @param affcaId
	 * @param affcaState
	 * @return
	 */
	@RequestMapping("update_holidaystate")
	@ResponseBody
	public Result_AddRule_Result updateHolidayState_(Long holidayId,Long holidayType){
		try {
			boolean b = dataDatumService.updateHolidayState(holidayId, holidayType);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 新增班次
	 * @param aff
	 * @return
	 */
	@RequestMapping("is_add_shift")
	@ResponseBody
	public Result_AddRule_Result addShift_(Shift aff,HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			
			boolean b = dataDatumService.AddShift(aff,staff.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 删除班次
	 * @param 
	 * @return
	 */
	@RequestMapping("is_del_shift")
	@ResponseBody
	public Result_AddRule_Result delShift_(Long shiftId){
		try {
			boolean b= dataDatumService.delShift(shiftId);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 修改班次状态
	 * @param 
	 * @param 
	 * @return
	 */
	@RequestMapping("update_shift_state")
	@ResponseBody
	public Result_AddRule_Result Updateshift_state(Long shiftId,Long shiftState){
		try {
			boolean b = dataDatumService.updateShift_state(shiftId, shiftState);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
		
		
	}
	/**
	 * 加载班次
	 */
	@RequestMapping("load_shift")
	@ResponseBody
	public Shift loadShift_(Long shiftId){
		return dataDatumService.LoadShift(shiftId);
	}
	/**
	 * 修改班次
	 * @param 
	 * @param 
	 * @return
	 */
	@RequestMapping("is_update_shift")
	@ResponseBody
	public Result_AddRule_Result updateShifts(Long shiftId,Shift s){
		try {
			boolean b = dataDatumService.updateShift(shiftId, s);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 查询所有的规则结果 json格式
	 * @return
	 */
	@RequestMapping("to_attendancerule")
	@ResponseBody
	public Map findAllAttendancerule(int page,int rows){
		long total =dataDatumService.pageByCount();//总记录数
		List<Attendancerule> list = dataDatumService.pageAttendancerule(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	/**
	 * 查询所有的规则结果 json格式
	 * @return
	 */
	@RequestMapping("to_attendanceruleByName")
	@ResponseBody
	public Map findAllAttendancerule(String attruName,int page,int rows){
		if(attruName==null||"".equals(attruName)){
			long total =dataDatumService.pageByCount();//总记录数
			List<Attendancerule> list = dataDatumService.pageAttendancerule(page, rows);
			Map map = new HashMap<>();
			map.put("total", total);
			map.put("rows", list);
			return map;
		}else{
			long total =dataDatumService.pageAttendanceruleByName(attruName,page, rows).size();//总记录数W
			List<Attendancerule> list = dataDatumService.pageAttendanceruleByName(attruName,page, rows);
			Map map = new HashMap<>();
			map.put("total", total);
			map.put("rows", list);
			return map;
		}
	}
	/**
	 * 查询所有事务类别
	 * @return
	 */
	@RequestMapping("to_affaircategoryss")
	@ResponseBody 
	public Map findAllAffaircategory(int page,int rows){
		Page<JSONArray> p=PageHelper.startPage(page,rows);
		dataDatumService.pageAffaircategory();
		/*long total = dataDatumService.pageByCountAffaircategory();
		List<Affaircategory> list = dataDatumService.pageAffaircategory(page, rows);*/
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", p.getResult());
		return map;
	}
	/**
	 * 查询所有的事务类别 json格式
	 * @return
	 */
@RequestMapping("to_affaircategoryByName")
	@ResponseBody
	public Map findAllAffaircategory(String affcaName,int page,int rows){
	Page<JSONArray> p=PageHelper.startPage(page,rows);
	dataDatumService.pageAffaircategoryByName(affcaName);
	Map map = new HashMap<>();
	map.put("total", p.getTotal());
	map.put("rows", p.getResult());
	return map;
	}
	/**
	 * 加载事务类别
	 */
	@RequestMapping("load_affaircategory")
	@ResponseBody
	public Affaircategory loadAffaircategory(Long affcaId){
		System.out.println(affcaId);
		return dataDatumService.loadAffaircategory(affcaId);
	}
	/**
	 * 修改事务类别
	 * @param affcaId
	 * @param aff
	 * @return
	 */
	@RequestMapping("is_update_affaircategory")
	@ResponseBody
	public Result_AddRule_Result updateAffaircategorys(Long affcaId,Affaircategory aff,HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			boolean b = dataDatumService.updateAffaircategory(affcaId, aff,staff.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 新增事务类别
	 * @param aff
	 * @return
	 */
	@RequestMapping("is_add_affaircategory")
	@ResponseBody
	public Result_AddRule_Result addAffaircategorys_(Affaircategory aff,HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			boolean b = dataDatumService.addAffaircategory(aff,staff.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 删除事务类别
	 * @param affcaId
	 * @return
	 */
	@RequestMapping("is_del_affaircategory")
	@ResponseBody
	public Result_AddRule_Result delAffaircategorys_(Long affcaId){
		try {
			boolean b= dataDatumService.delAffaircategory(affcaId);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 修改事务类别状态
	 * @param affcaId
	 * @param affcaState
	 * @return
	 */
	@RequestMapping("update_affaircategorystate")
	@ResponseBody
	public Result_AddRule_Result updateAffaircategoryState_(Long affcaId,Long affcaState){
		try {
			boolean b = dataDatumService.updateAffaircategoryState(affcaId, affcaState);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}

	/**
	 * 查询所有班次
	 * @return
	 */
	@RequestMapping("to_shift")
	@ResponseBody
	public List<Shift> findAllShift(){
		return dataDatumService.findAllShift();
	}
	/**
	 * 查询所有考勤卡
	 * @return
	 */
	@RequestMapping("to_timecard")
	@ResponseBody
	public List<Timecard> findAllTimecard(){
		return dataDatumService.findAllTimecard();
	}
	/**
	 * 新增规则结果
	 * @param attruName
	 * @param attruRepeat
	 * @param attruAffair
	 * @param attruRemarks
	 * @param attruState
	 * @param session
	 * @return
	 */
	@RequestMapping("to_add_rule_result")
	@ResponseBody
	public Result_AddRule_Result AddRuleResult(String attruName,String attruRepeat,String attruAffair,String attruRemarks,Integer attruState,HttpSession session){
		try {
			StaffInformation staff = (StaffInformation) session.getAttribute("staff");
			boolean b= dataDatumService.AddAttendancerule(attruName, attruRepeat, attruAffair, attruRemarks, attruState,staff.getStainName());
			
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 修改规则结果
	 * @param attruId
	 * @param att
	 * @return
	 */
	@RequestMapping("to_update_rule_result")
	@ResponseBody
	public Result_AddRule_Result UpdateRuleResult(Long attruId,Attendancerule att){
		try {
			boolean b = dataDatumService.UpdateAttendancerule(attruId, att);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
		
		
	}
	/**
	 * 修改规则结果状态
	 * @param attruId
	 * @param attruState
	 * @return
	 */
	@RequestMapping("update_rule_result_state")
	@ResponseBody
	public Result_AddRule_Result UpdateruleResult_state(Long attruId,Long attruState){
		try {
			boolean b = dataDatumService.updateAttendancerule_state(attruId, attruState);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
		
		
	}
	/**
	 * 根据编号删除规则结果
	 * @param attruId
	 * @return
	 */
	@RequestMapping("delete_rule_result_")
	@ResponseBody
	public Result_AddRule_Result deleteRuleResult_(Long attruId){
		try {
			boolean b= dataDatumService.deleteAttendancerule_rule_(attruId);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	@RequestMapping("load_attendancerule")
	@ResponseBody
	public Attendancerule findAttendancerule(Long attruId){
		return dataDatumService.LoadAttendancerule(attruId);
	}
	
	
}
